package com.wangyk.jpaproject.controller;

import com.wangyk.jpaproject.service.CategoryService;
import com.wangyk.rabbitmqcommon.VO.ResultVO;
import com.wangyk.rabbitmqcommon.model.jpaproject.Category;
import com.wangyk.rabbitmqcommon.utils.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 种类的相关接口
 * */
@Controller
@RequestMapping("/category")
@Slf4j
public class CategoryController
{
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增一个类别
     * */
    @RequestMapping("/add_category.do")
    @ResponseBody
    public ResultVO addCategory(@RequestBody Category category)
    {
        try
        {
            ResultVO resultVO=new ResultVO();
            Category newCategory=categoryService.addCategory(category);
            log.info("CategoryId:"+newCategory.getCategoryId().toString());
            log.info("CategoryName:"+newCategory.getCategoryName());
            resultVO.setData(newCategory);
            return ResultVOUtils.success(resultVO);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultVOUtils.error(null);
    }


    /**
     * 添加一个类别，修改一个类别
     * */
    @RequestMapping("/update_category.do")
    @ResponseBody
    public ResultVO updateCategory(@RequestBody Category category)
    {
        try
        {
            ResultVO resultVO=new ResultVO();
            Category newCategory=categoryService.updateCategory(category);
            log.info("CategoryId:"+newCategory.getCategoryId().toString());
            log.info("CategoryName:"+newCategory.getCategoryName());
            resultVO.setData(newCategory);
            return ResultVOUtils.success(resultVO);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultVOUtils.error(null);
    }

    /**
     * 添加一个类别，修改一个类别
     * */
    @RequestMapping("/find_all_category.do")
    @ResponseBody
    public ResultVO findAllCategory()
    {
        try
        {
            ResultVO resultVO=categoryService.findAllCategory();
            return resultVO;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResultVOUtils.error(null);
    }
}
